package com.huanzhidadi.java8inaction.chapter4;

import com.alibaba.fastjson2.JSON;
import com.huanzhidadi.java8inaction.model.Trader;
import com.huanzhidadi.java8inaction.model.Transaction;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class TransactionTest {

    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        // 找出2011年的所有交易并按交易额排序
        List<Transaction> tr2011 = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        log.info(JSON.toJSONString(tr2011));
        System.out.println("-----------------------------------------");

        // 交易员都在哪些不同的城市工作过
        List<String> cities = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());
        log.info(JSON.toJSONString(cities));
        System.out.println("-----------------------------------------");

        // 查找所有来自于剑桥的交易员，并按姓氏排序
        List<Trader> traders = transactions.stream()
                .map(Transaction::getTrader)
                .filter(trader -> trader.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        log.info(JSON.toJSONString(traders));
        System.out.println("-----------------------------------------");

        // 返回所有交易员的姓名字符串，按字母顺序排序
        String traderStr = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2 + " ");
        log.info(traderStr);
        System.out.println("-----------------------------------------");

        // 有没有交易员是在米兰工作的
        boolean milanBased = transactions.stream()
                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
        log.info("有没有交易员是在米兰工作的: " + milanBased);
        System.out.println("-----------------------------------------");

        // 打印生活在剑桥的交易员的所有交易额
        transactions.stream()
                .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        System.out.println("-----------------------------------------");

        // 所有交易中，最高的交易额是多少
        transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max)
                .ifPresent(System.out::println);
        System.out.println("-----------------------------------------");

        // 找到交易额最小的交易
        transactions.stream()
                .min(Comparator.comparing(Transaction::getValue))
                .ifPresent(d -> log.info(JSON.toJSONString(d)));
        System.out.println("-----------------------------------------");


    }
}
